Skip to content

feat(go): add configurable size guardrails#3475

Merged
chaokunyang merged 16 commits intoapache:mainfrom
ayush00git:fix/size-guardrails
Mar 19, 2026
Merged

feat(go): add configurable size guardrails#3475
chaokunyang merged 16 commits intoapache:mainfrom
ayush00git:fix/size-guardrails

Conversation

@ayush00git
Copy link
Copy Markdown
Contributor

@ayush00git ayush00git commented Mar 13, 2026

Why?

go deserialization didn't have any configured guardrails for untrusted paylaods which leads to high memory pressure while allocation and Out of memory attacks.

What does this PR do?

Added two configurable guardrails MaxCollectionSize and MaxBinarySize, and implement size guardrails across the fory codegen.

  1. Configuration: Added MaxCollectionSize and MaxBinarySize to Config struct with corresponding options WithMaxCollectionSize and WithMaxBinarySize.
  2. Added ReadCollectionLength and ReadBinaryLength to ByteBuffer and ReadContext.
  3. These methods enforce configured limits and return specialized errors: ErrKindMaxCollectionSizeExceeded and ErrKindMaxBinarySizeExceeded.
  4. Updated the Go code generator (codegen/decoder.go) to use these guarded length methods in generated serializers.
  5. Removed the generic ReadLength method to ensure all length-reading paths in the codebase are subject to guardrails.

Related issues

Closes #3419

AI Contribution Checklist

  • Substantial AI assistance was used in this PR: yes

  • If yes, I included a completed AI Contribution Checklist in this PR description and the required AI Usage Disclosure.

  • If yes, I included the standardized AI Usage Disclosure block below.

  • If yes, I can explain and defend all important changes without AI help.

  • If yes, I reviewed AI-assisted code changes line by line before submission.

  • If yes, I ran adequate human verification and recorded evidence (checks run locally or in CI, pass/fail summary, and confirmation I reviewed results).

  • If yes, I added/updated tests and specs where required.

  • If yes, I validated protocol/performance impacts with evidence when applicable.

  • If yes, I verified licensing and provenance compliance.

AI Usage Disclosure
I used AI to find and replace the multiple iterations of `ReadLength` by the specific `ReadCollectionSize` / `ReadBinarySize` across the go runtime.
Also I used it to fix some errors during running tests.
I can still explain all of my work, as everything is tested by me.

Does this PR introduce any user-facing change?

  • Does this PR introduce any public API change?
  • Does this PR introduce any binary protocol compatibility change?

Benchmark

@ayush00git ayush00git requested a review from chaokunyang as a code owner March 13, 2026 17:23
@ayush00git ayush00git changed the title added guardrails and their type Errors feat(go): add configurable size guardrails Mar 13, 2026
@ayush00git
Copy link
Copy Markdown
Contributor Author

Hii @chaokunyang
have a look and let me know if there are any changes

@ayush00git
Copy link
Copy Markdown
Contributor Author

@chaokunyang
i have replaced the collection and binary semantics to ReadContext now, have a look and let me know the changes

@ayush00git
Copy link
Copy Markdown
Contributor Author

hi @chaokunyang
I want to clear myself, that config calls was just my confusion, because i thought they needed to be set during deserialization, I didn't noticed that it was immutable with NewFory and we didn't needed to set it again. I have fixed it.

@ayush00git
Copy link
Copy Markdown
Contributor Author

If there is still anything wrong in my implementation, please do drop me a design to follow.

@ayush00git
Copy link
Copy Markdown
Contributor Author

ayush00git commented Mar 18, 2026

@chaokunyang
I set default values to it, now have a look and let me know the changes

Copy link
Copy Markdown
Collaborator

@chaokunyang chaokunyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chaokunyang chaokunyang merged commit fd99530 into apache:main Mar 19, 2026
58 checks passed
@ayush00git
Copy link
Copy Markdown
Contributor Author

LGTM

Hii @chaokunyang
I had sent you a draft for a review a week ago following our slack conversation, could you let me know the status of review pls. you can check your slack and mail.

@ayush00git ayush00git deleted the fix/size-guardrails branch March 22, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Go] configurable size guardrails for untrusted payloads

2 participants